import { DictTypeModel } from '../models/dictType'
import { Request, Response } from 'express';
import { UserModel } from '../models/user'
export class DictTypeController {
  private dictTypeModel: DictTypeModel;
  private userModel: UserModel;
  constructor() {
    this.dictTypeModel = new DictTypeModel();
    this.userModel = new UserModel();
  }
  add = async (req: Request, res: Response): Promise<void> => {
    try {
      const user = req.user!;
      const { type_code, type_name, remark } = req.body;
      const result = await this.dictTypeModel.create({ type_code, type_name, remark, create_by: user.userId, update_by: user.userId });
      if (result) {
        res.json({ message: '添加成功', code: 200 });
      } else {
        res.status(200).json({ code: 400, message: '添加失败' })
      }
    }
    catch (error) {
      res.status(200).json({ code: 500, message: '服务器错误' });
    }
  }
  delete = async (req: Request, res: Response): Promise<void> => {
    try {
      const { ids } = req.body;
      const user = req.user!;
      let result
      if (user.isAdmin) {
        result = await this.dictTypeModel.delete(ids);
      } else {
        result = await this.dictTypeModel.delete(ids, user.userId);
      }
      if (result.changedRows > 0) {
        res.json({ message: '删除成功', code: 200 });
      } else {
        res.status(200).json({ code: 400, message: '删除失败,请删除自己创建的数据！' })
      }
    } catch (error) {
      console.log(error);
      res.status(200).json({ code: 500, message: '服务器错误' });
    }
  }
  getDictTypeList = async (req: Request, res: Response): Promise<void> => {
    try {
      const { page, pageSize } = req.query;
      const result = await this.dictTypeModel.getDictTypeList(Number(page), Number(pageSize));
      if (result.total === 0) {
         res.json({
          code: 200,
          message: '查询成功',
          data: result,
        })
        return
      }
      const createUserNames = await this.userModel.getUsersByUserIds(result.rows.map(item => item.create_by))
      const upDateUserNames = await this.userModel.getUsersByUserIds(result.rows.map(item => item.update_by))
      const relUserNames = new Map();
      createUserNames.forEach(item => {
        relUserNames.set(item.userId, item)
      })
      upDateUserNames.forEach(item => {
        if (relUserNames.has(item.userId)) return;
        relUserNames.set(item.userId, item)
      })
      result.rows = result.rows.map(item => {
        return {
          ...item,
          create_by: relUserNames.get(item.create_by)?.username,
          update_by: relUserNames.get(item.update_by)?.username
        }
      })
      res.json({
        code: 200,
        message: '查询成功',
        data: result,
      });
    } catch (error) {
      console.log(error);
      res.status(200).json({ code: 500, message: '服务器错误' });
    }
  }
  update = async (req: Request, res: Response): Promise<void> => {
    try {
      const user = req.user!;
      const { id, type_code, type_name, remark } = req.body;
      await this.dictTypeModel.update({ id, type_code, type_name, remark, update_by: user.userId });
      res.json({ message: '更新成功', code: 200 });
    } catch (error) {
      res.status(200).json({ code: 500, message: '服务器错误' });
    }
  }
  findByCode = async (req: Request, res: Response): Promise<void> => {
    try {
      const { type_code } = req.query;
      const result = await this.dictTypeModel.findByCode(type_code as string);
      res.json({
        code: 200,
        message: '查询成功',
        data: result.length > 0 ? result[0] : null
      });
    } catch (error) {
      res.status(200).json({ code: 500, message: '服务器错误' });
    }
  }
  findById = async (req: Request, res: Response): Promise<void> => {
    try {
      const { id } = req.query;
      const result = await this.dictTypeModel.findById(Number(id));
      res.json({
        code: 200,
        message: '查询成功',
        data: result
      });
    } catch (error) {
      res.status(200).json({ code: 500, message: '服务器错误' });
    }
  }
}